<?php


namespace app\dbtool;

use app\msgapi\model\HuihuaTables;
use \think\facade\Db;
use think\facade\Log;

class tableTool
{
    public function createTable($date)
    {
        if (!$date) return false;
        //创建会话对象表
//        $group_res = $this->createGroupTable($corpid);
        //创建会话表
        $msg_res = $this->createMsgTable($date);
//        return $msg_res&&$group_res;
        return $msg_res;
    }


    private function createMsgTable($date){
        try {
            //判断表是否存在
            $tableName = "hs_huihua_msg_" . strtolower($date);
            $checkExsitSql = "show tables like '{$tableName}'";
            $res = Db::query($checkExsitSql);
            if (!$res) {
                $sql = file_get_contents(app()->getBasePath() . '/dbtool/hs_huihua_msg.sql');
                $sql = str_replace('`hs_huihua_msg`', '`' . $tableName . '`', $sql);
                Db::query($sql);
            }
            //记录表中的记录如果不存在则记录,防止记录不上.
            if(HuihuaTables::where('table_name',$tableName)->count() < 1){
                $create_data = [
                    'table_name'=>$tableName,
                    'table_suffix'=>strtolower($date),
                    'isdecrtpt_all'=>0,
                    'add_time'=>date('Y-m-d H:i:s')
                ];
                HuihuaTables::create($create_data); //记录表
            }
            $res = Db::query($checkExsitSql);
            if ($res) return true;
            return false;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * @param $corpid
     * @return bool
     */
    /*private function createGroupTable($corpid)
    {
        try {
            //判断表是否存在
            $tableName = "hs_huihua_group_" . strtolower($corpid);
            $checkExsitSql = "show tables like '{$tableName}'";
            $res = Db::query($checkExsitSql);
            if (!$res) {
                $sql = file_get_contents(app()->getBasePath() . '/magic/dbpool/hs_huihua_group.sql');
                $sql = str_replace('`hs_huihua_group`', '`' . $tableName . '`', $sql);
                Db::query($sql);
            }
            $res = Db::query($checkExsitSql);
            if ($res) {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            return false;
        }
    }*/

}